package com.happyscan138.makefun.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.happyscan138.makefun.BaseAdapter.MyCollectionvideo;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.VideoviewPlay;
import com.happyscan138.makefun.View.RoundImageView;
import com.happyscan138.makefun.base.VideoPlayView;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2017/5/28.
 */

public class MyCollectionVideoAdapter extends BaseAdapter implements VideoPlayView.MediaPlayerImpl {
    private  Context mContext;
    private List<MyCollectionvideo> mdata;
    private String access_key;
    private boolean isPlaying=false;
    private MyCollectionvideo bean;
    public MyCollectionVideoAdapter(Context mContext, List<MyCollectionvideo> mdata,String access_key) {
        Log.e("mycollection","mycollection");
        this.mContext = mContext;
        this.mdata = mdata;
        this.access_key=access_key;
    }

    @Override
    public int getCount() {
        return mdata.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {

        final  ViewHolder_MyCollection_VIDEO viewHolder;
        if(view==null){
            //dsdsds
            viewHolder=new ViewHolder_MyCollection_VIDEO();
            view=View.inflate(mContext, R.layout.my_collection_video_item,null);
            viewHolder.MyCollection_VIDEO_head=(RoundImageView) view.findViewById(R.id.my_collection_video_head);
            viewHolder.MyCollection_VIDEO_name=(TextView) view.findViewById(R.id.my_collection_video_name);
            viewHolder.MyCollection_VIDEO_cacel=(Button)view.findViewById(R.id.video_CancelCollection) ;
            viewHolder.MyCollection_VIDEO_playView=(VideoPlayView)view.findViewById(R.id.mycollection_video_play_view);
            viewHolder.MyCollection_VIDEO_showLayout=(FrameLayout)view.findViewById(R.id.mycollection_show_layout);
            viewHolder.MyCollection_VIDEO_play=(ImageView) view.findViewById(R.id.mycollection_video_play_btn);
            viewHolder.MyCollection_VIDEO_nicheng=(TextView) view.findViewById(R.id.my_collection_video_nicheng);
            viewHolder.MyCollection_VIDEO_text=(TextView) view.findViewById(R.id.my_collection_video_text);

            //将viewHolder与converView绑定
            view.setTag(viewHolder);
        }else {
            //如果存在converView，取出保存在converView中的viewHolder
            viewHolder=(ViewHolder_MyCollection_VIDEO) view.getTag();
        }
       bean=mdata.get(position);
        Picasso.with(mContext).load(bean.head).into(viewHolder.MyCollection_VIDEO_head);
        Log.e("objectDataimg_path",bean.head);
        viewHolder.MyCollection_VIDEO_name.setText(bean.nicheng);
        viewHolder.MyCollection_VIDEO_nicheng.setText(bean.name);
        viewHolder.MyCollection_VIDEO_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                //VideoPlay类的播放器，适合播放买m3u8格式文件，VideoPlayView播放器适合播放.MP4加载时间长的视频文件
                intent.putExtra("url",mdata.get(position).resource);
                intent.setClass(mContext,VideoviewPlay.class);
                mContext.startActivity(intent);
            }
        });

        viewHolder.MyCollection_VIDEO_text.setText(bean.text);
        viewHolder.MyCollection_VIDEO_cacel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                params=new RequestParams();
                params.put("access_key",access_key);
                params.put("like_id",mdata.get(position).like_id);
                log.e("cancel",params.toString());
                client.post(Urls.CancelCollection,params,new JsonHttpResponseHandler(){
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        super.onSuccess(statusCode, headers, response);
                        String jsonStr=response.toString();
                        log.e("jsonStr",jsonStr);
                        try {
                            JSONObject jsonObject=new JSONObject(jsonStr);
                            String code=jsonObject.getString("code");
                            int code_int=Integer.valueOf(code).intValue();
                            if(code_int==1) {
                                mdata.remove(position);
                                notifyDataSetChanged();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
        return view;
    }
    public void setPlayView(VideoPlayView playView) {
        playView.setMediaPlayerListenr(this);
    }

    @Override
    public void onError() {

    }

    @Override
    public void onExpend() {

    }

    @Override
    public void onShrik() {

    }
}

class ViewHolder_MyCollection_VIDEO{
    public RoundImageView MyCollection_VIDEO_head;
    public TextView MyCollection_VIDEO_name;
    public Button MyCollection_VIDEO_cacel;
    public VideoPlayView MyCollection_VIDEO_playView;
    public FrameLayout MyCollection_VIDEO_showLayout;
    public ImageView MyCollection_VIDEO_play;
    public TextView MyCollection_VIDEO_nicheng;
    public TextView MyCollection_VIDEO_text;


}
